﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using HardStyle.SimpleEventBus.Registration;
using Microsoft.Extensions.DependencyInjection;

namespace HardStyle.SimpleEventBus
{
    public static class ServiceCollectionExtensions
    {
        internal static IServiceCollection AddSimpleEventBus(this IServiceCollection services, SimpleEventBusServiceConfiguration configuration, IEnumerable<Assembly> assemblies)
        {
            if (assemblies.Count() is 0)
            {
                throw new ArgumentOutOfRangeException(nameof(assemblies), 0, "0 is not allowed");
            }

            CoreServiceRegistrar.Registrar(services, configuration);
            HandlerRegistrar.Registrar(services, assemblies, configuration);

            return services;
        }


        public static IServiceCollection AddSimpleEventBus(this IServiceCollection services, Action<SimpleEventBusServiceConfiguration>? configuration, IEnumerable<Assembly> assemblies)
        {
            var conf = new SimpleEventBusServiceConfiguration();
            configuration?.Invoke(conf);
            return AddSimpleEventBus(services, conf, assemblies);
        }

        public static IServiceCollection AddSimpleEventBus(this IServiceCollection services, Action<SimpleEventBusServiceConfiguration>? configuration, IEnumerable<Type> handleAssemblyTypes)
            => services.AddSimpleEventBus(configuration, handleAssemblyTypes.Select(x => x.Assembly));


        public static IServiceCollection AddSimpleEventBus(this IServiceCollection services, Action<SimpleEventBusServiceConfiguration>? configuration, params Assembly[] assemblies)
         => services.AddSimpleEventBus(configuration, assemblies.AsEnumerable());

        public static IServiceCollection AddSimpleEventBus(this IServiceCollection services, Action<SimpleEventBusServiceConfiguration>? configuration, params Type[] handleAssemblyTypes)
         => services.AddSimpleEventBus(configuration, handleAssemblyTypes.Select(x => x.Assembly));


        public static IServiceCollection AddSimpleEventBus(this IServiceCollection services, params Assembly[] assemblies)
           => services.AddSimpleEventBus(null, assemblies);

        public static IServiceCollection AddSimpleEventBus(this IServiceCollection services, params Type[] handleAssemblyTypes)
           => services.AddSimpleEventBus(null, handleAssemblyTypes);












    }
}
